//根据姓名译出拼音索引、四角号码
function gencode(obj)
{
	//changeStyle(obj,'','','');
	if (!isEmpty(obj.value))
	{
		sendHttpRequest("jsp/hams/chineseDecode.jsp","name="+obj.value,"getResult")
	}
	else
	{
		document.getElementById("HRARCHIVELISTEdit_guid_report1_wxcol_SPELLINDEX").value="";
		document.getElementById("HRARCHIVELISTEdit_guid_report1_wxcol_FOURCORNER").value="";
	}
}

function getResult()
{
	if (request.readyState==4 && request.status==200)
	{
		var root=request.responseXML.documentElement;
		if (root)
		{
			document.getElementById("HRARCHIVELISTEdit_guid_report1_wxcol_SPELLINDEX").value=root.childNodes[0].firstChild.nodeValue;
			document.getElementById("HRARCHIVELISTEdit_guid_report1_wxcol_FOURCORNER").value=root.childNodes[1].firstChild.nodeValue;
		}
		else
		{
			document.getElementById("HRARCHIVELISTEdit_guid_report1_wxcol_SPELLINDEX").value="";
			document.getElementById("HRARCHIVELISTEdit_guid_report1_wxcol_FOURCORNER").value="";
		}
	}
}

//检查身份证号是否重复
function checkIdCard(obj)
{
	//changeStyle(obj,'','','');
	if (!isEmpty(obj.value))
	{
		sendHttpRequest("jsp/hams/checkID.jsp","idCard="+obj.value,"showResult")
	}
	else
	{
		//document.getElementById("errorDiv").innerHTML="&nbsp;";
	}
}

function showResult()
{
	if (request.readyState==4 && request.status==200)
	{
		var text=request.responseText;
		text = trim(text);
		text = removeCr(text);
		//document.getElementById("errorDiv").innerHTML=text;
		//document.getElementById("errorSpan").innerHTML="";

		if (text.length==0)
		{
			var idCard=document.getElementById("HRARCHIVELISTEdit_guid_report1_wxcol_IDCARD").value;
			
			var tempDate="";
			if (idCard.length==15)
			{
				tempDate="19"+idCard.substr(6,6);
			}
			else if (idCard.length==18)
			{
				tempDate=idCard.substr(6,8);
			}
			document.getElementById("HRARCHIVELISTEdit_guid_report1_wxcol_BIRTHDAY").value=(isDate(tempDate)?tempDate:"");
		}
		else
		{
			alert(text.length);
			alert("*" + text + "*");
		}
	}
}

function trim(str)
{
	var result = str;
	while (result.indexOf(" ")>=0)
	{
		result = result.replace(" ","");
	}
	return result;
}

function removeCr(str)
{
	var result = str;
	while (result.indexOf("\n")>=0)
	{
		result = result.replace("\n","");
	}
	while (result.indexOf("\r")>=0)
	{
		result = result.replace("\r","");
	}
	return result;
}

function isDate(str)
{
	var result=false;
	if (isNumeric(str)) 
	{
		switch (str.length)
		{
			case 4:
				var year=str;
				var d=new Date(year,'01','01');
				if (d.getFullYear()==year)
				{
					result=true;
				}
				break;
			case 6:
				var year=str.substr(0,4);
				var month=str.substr(4,2)-1;
				var d=new Date(year,month,'01');
				if (d.getFullYear()==year && d.getMonth()==month)
				{
					result=true;
				}
				break;
			case 8:
				var year=str.substr(0,4);
				var month=str.substr(4,2)-1;
				var day=str.substr(6,2);
				var d=new Date(year,month,day);
				if ((d.getFullYear()==year) && (d.getMonth()==month) && (d.getDate()==day))
				{
					result=true;
				}
				break;
		}
	}
	
	return result;
}



//弹出代码选择模态窗口
function showCode(classNo,nameId,codeId)
{
	var name=document.getElementById(nameId).value;
	var code=document.getElementById(codeId).value;
	var obj=new Object();
	obj.name=name;
	obj.code=code;
	
	var url="codeServlet?classNo="+classNo;
	var style="dialogWidth:380px;dialogHeight:475px;"
	var results=window.showModalDialog(url,obj,style);
	if (results)
	{
		document.getElementById(nameId).value=results[0];
		document.getElementById(codeId).value=results[1];
	}
}

//多列计算
function calprofit(trObjsArr){
	var trObj = trObjsArr.parentNode.parentNode.parentNode.parentNode;
	var tdObj = trObj.getElementsByTagName('TD');
	var am_saleprice = 0;
	var am_rate = 0;
	var am_pay = 0;
	var goods_cost = 0;
	var am_shipperprice = 0;
	for( i = 0; i < tdObj.length; i++ ) {        
		if(tdObj[i].getAttribute('value_name')=='am_saleprice' ){
			if(trObjsArr.name.indexOf('am_saleprice')==-1){
				am_saleprice = tdObj[i].getAttribute('value');
			}else{
				am_saleprice = trObjsArr.value;
			}

		}
		 
		if(tdObj[i].getAttribute('value_name')=='am_rate'){
			if(trObjsArr.name.indexOf('am_rate')==-1){
				am_rate = tdObj[i].getAttribute('value');
			}else{
				am_rate = trObjsArr.value;
			}
		 
		}

		if(tdObj[i].getAttribute('value_name')=='am_pay'){
			if(trObjsArr.name.indexOf('am_pay')==-1){
				am_pay = tdObj[i].getAttribute('value');   
			}else{
				am_pay = trObjsArr.value;
			}

		}


		if(tdObj[i].getAttribute('value_name')=='goods_cost'){
			if(trObjsArr.name.indexOf('goods_cost')==-1){
				goods_cost = tdObj[i].getAttribute('value');
			}else{
				goods_cost = trObjsArr.value;
			}
		 
		}


		if(tdObj[i].getAttribute('value_name')=='am_shipperprice'){
			if(trObjsArr.name.indexOf('am_shipperprice')==-1){
				am_shipperprice = tdObj[i].getAttribute('value');  
			}else{
				am_shipperprice = trObjsArr.value;
			}

		}

		if(tdObj[i].getAttribute('value_name')=='am_profit'){
			var profit = am_saleprice * am_rate - goods_cost - am_shipperprice - am_saleprice * am_pay;
			tdObj[i].setAttribute('value',profit.toFixed(2));
			tdObj[i].innerHTML = profit.toFixed(2);
		}

	}
}